package com.woniuxy.woniuhis.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.woniuhis.entity.Inpatient;
import com.woniuxy.woniuhis.entity.Outpatient;
import com.woniuxy.woniuhis.entity.Patient;
import com.woniuxy.woniuhis.entity.RegisInfo;
import com.woniuxy.woniuhis.exception.OutpatientServiceException;
import com.woniuxy.woniuhis.mapper.OutpatientMapper;
import com.woniuxy.woniuhis.mapper.RegisInfoMapper;
import com.woniuxy.woniuhis.service.OutpatientService;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Service
public class OutpatientServiceImpl implements OutpatientService{

	@Autowired
	OutpatientMapper outpatientMapper;
	@Autowired
	RegisInfoMapper regisInfoMapper;
	
	@Override
	public void addOutpatientByRegisInfoId(String regisInfoId) {
		try {
			//根据挂号id查询出挂号信息
			RegisInfo regisInfo = regisInfoMapper.selectRegisInfoById(regisInfoId);
			//初始化一条门诊信息
			Outpatient outpatient = new Outpatient();
			//设置主键
			outpatient.setOutpatientId(UUIDUtil.uuid32());
			//设置患者id
			outpatient.setPatientId(regisInfo.getPatientId());
			//设置科室id
			outpatient.setOfficeId(regisInfo.getOfficeId());
			//初始化开始结束状态
			outpatient.setStart_status(false);
			outpatient.setEnd_status(false);
			//插入一条门诊信息
			outpatientMapper.insertOutpatient(outpatient);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OutpatientServiceException("系统维护中");
		}
	}

	@Override
	public ResponseJson<Outpatient> findAllNotStartOutpatient() {
		try {
			//查询还未就诊的门诊信息
			List<Outpatient> allNotStartOutpatients = outpatientMapper.selectAllNotStart();
			//创建layui数据表格能识别的数据结构
			ResponseJson<Outpatient> responseJson = new ResponseJson<Outpatient>();
			responseJson.setCode(0);
			responseJson.setCount(allNotStartOutpatients.size());
			responseJson.setData(allNotStartOutpatients);
			responseJson.setMsg("");
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			throw new OutpatientServiceException("系统维护中");
		}
	}

	@Override
	public ResponseJson<Outpatient> findAllOutpatientByPid(String patientId) {
		try {
			List<Outpatient> outpatients = outpatientMapper.selectOutpatientByPatientId(patientId);
			System.out.println(outpatients);
			//创建layui数据表格能识别的数据结构
			ResponseJson<Outpatient> responseJson = new ResponseJson<Outpatient>();
			responseJson.setCode(0);
			responseJson.setCount(outpatients.size());
			responseJson.setData(outpatients);
			responseJson.setMsg("");
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			throw new OutpatientServiceException("系统维护中");
		}
	}

	@Override
	public void savePatientDescByOid(String outpatientId, String patientDesc) {
		try {
			Outpatient outpatient = new Outpatient();
			outpatient.setOutpatientId(outpatientId);
			outpatient.setPatientDesc(patientDesc);
			outpatientMapper.updateOutpatientById(outpatient);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OutpatientServiceException("系统维护中");
		}
	}

	@Override
	public void startOutpatientByOid(String outpatientId) {
		try {
			Outpatient outpatient = new Outpatient();
			outpatient.setOutpatientId(outpatientId);
			outpatient.setStart_status(true);
			outpatientMapper.updateOutpatientById(outpatient);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OutpatientServiceException("系统维护中");
		}
	}

	@Override
	public Integer outpatientStatus(boolean b, boolean c) {
		try {
			Integer count = outpatientMapper.outpatientStatus(b,c);
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			throw new OutpatientServiceException("系统维护中");
		}
	}

	@Override
	@Transactional
	public void outpatientEnd(String outpatientId, Outpatient outpatient) {
		try {
			//更新门诊信息
			outpatient.setOutpatientId(outpatientId);
			outpatient.setOutpatientDate(new Date());
			outpatient.setEnd_status(true);
			outpatientMapper.updateOutpatientById(outpatient);
			//如果患者需要住院，则插入一条住院信息
			if(outpatient.getIsInhospital()) {
				//获得患者id
				Outpatient outpatientById = outpatientMapper.selectOutpatientById(outpatientId);
				Patient patient = new Patient();
				patient.setPatientId(outpatientById.getPatientId());
				Inpatient inpatient = new Inpatient(UUIDUtil.uuid32(), null, patient, null, null, null, null, null, null, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OutpatientServiceException("系统维护中");
		}
		
	}
	
}
